package com.example.news.mvp.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.news.R;
import com.example.news.utils.Utils;

import java.util.List;

public class MainPagerAdapter extends FragmentPagerAdapter {
    private String[] tabNames;
    private List<Fragment> list;

    public MainPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    public View getTabView(Context context, int position) {
        if (null == tabNames) {
            tabNames = context.getResources().getStringArray(R.array.tabs);
        }
        View tabView = View.inflate(context, R.layout.tab_indicator, null);
        TextView textView = Utils.getView(tabView, R.id.tab_title);
        textView.setText(tabNames[position]);
        return tabView;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position < 0 || position >= tabNames.length) {
            return super.getPageTitle(position);
        }
        return tabNames[position];
    }
}
